package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import sun.jdbc.rowset.CachedRowSet;

public class oracle {
    private Connection conn = null;

    private Statement ps = null;

    private ResultSet rs = null;

    private oracleConnector db = new oracleConnector();

    private PreparedStatement prs = null;

    public static long num = 0; // ͳ����ݿ������

    /**
	 * 
	 * @param sql
	 * @return
	 * @throws java.sql.SQLException
	 */
    public CachedRowSet executeQuery(String sql) throws java.sql.SQLException {
        CachedRowSet cst = new CachedRowSet();
        try {
            conn = db.getConnection();
            ps = conn.createStatement();
            ps.setFetchSize(100);
            rs = ps.executeQuery(sql);
            cst.populate(rs);
            num++;
        } catch(Exception e) {
        	System.err.println(sql);
            throw new SQLException("��ݲ�ѯ���" + e.getMessage() + "���sqlΪ��" + sql);
        } finally {
            this.clean();
        }
        return cst;
    }

    /**
	 * 
	 * @param sql
	 * @throws SQLException
	 */
    public void execute(String sql) throws SQLException {
        try {
            if (sql.contains("drop") || sql.contains("truncate")) {
                System.out.print("����sqlΪ:" + sql + ";�������ȫ���룡");
            } else {
                conn = db.getConnection();
                ps = conn.createStatement();
                ps.execute(sql);
                ps.close();
                num++;
            }
        } catch(SQLException e) {
            throw new SQLException(e.getMessage() + "����sqlΪ:" + sql);
        } finally {
            clean();
        }
    }

    /**
	 * ����
	 * 
	 * @param sqlList
	 * @throws SQLException
	 */
    @SuppressWarnings("unused") public void execute(ArrayList sqlList) throws SQLException {
        String sql = "";
        try {
            boolean autostatus;
            conn = db.getConnection();
            autostatus = conn.getAutoCommit();
            conn.setAutoCommit(false);
            ps = conn.createStatement();
            Iterator it = sqlList.iterator();
            while (it.hasNext()) {
                sql = (String) it.next();
                ps.execute(sql);
            }
            ps.close();
            num++;
            if (autostatus) {
                conn.commit();
            }

        } catch(SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            clean();
        }
    }

    /**
	 * 
	 * @Transaction
	 * 
	 */
    public void startTransaction() throws SQLException {
        if (conn != null) {
            conn.setAutoCommit(false);
        }
    }

    public void commit() {
        try {
            if (conn != null) {
                conn.commit();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollback() {
        try {
            if (conn != null) {
                conn.rollback();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }

    public void clean() {
        if (this.prs != null) {
            try {
                this.prs.close();
            } catch(SQLException e) {
                e.printStackTrace();
            } finally {
                this.prs = null;
            }
        }
        if (this.rs != null) {
            try {
                this.rs.close();
            } catch(SQLException e) {
                e.printStackTrace();
            } finally {
                this.rs = null;
            }
        }
        if (this.ps != null) {
            try {
                this.ps.close();
            } catch(SQLException e) {
                e.printStackTrace();
            } finally {
                this.ps = null;
            }
        }
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            } finally {
                this.conn = null;
            }
        }
    }

}